<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <el-row>
        <el-col :span="3">
          <el-button type="primary" @click="handelAddCateBtn">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <tree-table
        :data="cateLists"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        show-index
        index-text="#"
        border
        :show-row-hover="false"
      >
        <!-- 是否有效 -->
        <template slot="isok" slot-scope="scope">
          <i
            v-if="scope.row.cat_deleted === false"
            class="el-icon-success"
            style="color: lightgreen"
          ></i>
          <i v-else class="el-icon-error" style="color: red"></i>
        </template>
        <!-- 排序 -->
        <template slot="order" slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag size="mini" type="success" v-else-if="scope.row.cat_level === 1">二级</el-tag>
          <el-tag size="mini" type="warning" v-else>三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot="opt" slot-scope="scope">
          <el-button size="mini" type="primary" icon="el-icon-edit" @click="editCates(scope.row.cat_id)">编辑</el-button>
          <el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
        </template>
      </tree-table>
      <!-- 页码 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryParams.pagenum"
        :page-sizes="[3, 5, 10, 15]"
        :page-size="queryParams.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 添加商品对话框 -->
    <el-dialog title="添加商品分类" :visible.sync="addCtesDialogVisible" width="50%" @close="closeCatesDialog">
      <!-- 添加分类的表单 -->
      <el-form
        :model="addCtesRuleForm"
        :rules="addCtesRuleFormRules"
        ref="addCtesRuleFormRef"
        label-width="100px"
      >
        <el-form-item label="分类名称:" prop="cat_name">
          <el-input v-model="addCtesRuleForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类:">
          <!-- 级联选择器 -->
          <!-- options数据 -->
          <el-cascader
            :options="catesListsOptions"
            :props="catesProps"
            @change="catesparentChange"  v-model="catesKeys">
         </el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCtesDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCates">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 请求参数
      queryParams: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      // 存储的商品数据
      cateLists: [],
      //   数据条数
      total: 0,
      // 配置表格的属性
      columns: [
        { label: '分类名称', prop: 'cat_name' },
        { label: '是否有效', type: 'template', template: 'isok' },
        { label: '排序', type: 'template', template: 'order' },
        { label: '操作', type: 'template', template: 'opt' }
      ],
      // 显示与隐藏添加商品分类对话框
      addCtesDialogVisible: false,
      // 添加分类的表单数据
      addCtesRuleForm: {
        cat_name: '',
        //  父级id
        cat_pid: 0,
        //   默认添加一级分类
        cat_level: 0
      },
      // 添加商品分类的表单验证
      addCtesRuleFormRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { min: 2, max: 8, message: '长度在 2到 8 个字符', trigger: 'blur' }
        ]
      },
      //  父级数据
      catesListsOptions: [],
      //   父级Id数组
      catesKeys: [],
      //   父级级联选择器props
      catesProps: {
        expandTrigger: 'hover',
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      // 修改的参数
      editCatesLists: {}
    }
  },
  methods: {
    async getCateLists () {
      const { data: res } = await this.$http.get('categories', {
        params: this.queryParams
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品分类数据失败')
      }
      this.cateLists = res.data.result
      this.total = res.data.total
    },
    // 监听页码的改变
    handleSizeChange (newSize) {
      this.queryParams.pagesize = newSize
      this.getCateLists()
    },
    // 监听pagenum的改变
    handleCurrentChange (newPage) {
      this.queryParams.pagenum = newPage
      this.getCateLists()
    },
    // 添加商品按钮
    handelAddCateBtn () {
      this.getParentCateLists()
      this.addCtesDialogVisible = true
    },
    // 获取父级分类的数据列表
    async getParentCateLists () {
      const { data: res } = await this.$http.get('categories', { params: { type: 2 } })
      if (res.meta.status !== 200) {
        return this.$message.error('获取父级分类的数据失败')
      }
      this.catesListsOptions = res.data
    },
    //  当级联选择器的值发生改变的时候触发这个事件
    catesparentChange () {
      console.log(this.catesKeys)
      if (this.catesKeys.length > 0) {
        this.addCtesRuleForm.cat_pid = this.catesKeys[this.catesKeys.length - 1]
        this.addCtesRuleForm.cat_level = this.catesKeys.length
      } else {
        this.addCtesRuleForm.cat_pid = 0
        this.addCtesRuleForm.cat_level = 0
      }
    },
    // 点击按钮，提交添加分类
    addCates () {
      this.$refs.addCtesRuleFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('categories', this.addCtesRuleForm)
        if (res.meta.status !== 201) {
          return this.$message.error('添加分类失败')
        }
        this.$message.success('添加分类成功')
        this.addCtesDialogVisible = false
      })
    },
    // 关闭cates对话框
    closeCatesDialog () {
      this.$refs.addCtesRuleFormRef.resetFields()
      this.catesKeys = []
      this.addCtesRuleForm = {}
    },
    // 编辑分类按钮
    async editCates (id) {
      const { data: res } = await this.$http.get('categories/' + id)
      if (res.meta.status !== 200) {
        return this.$message.error('获取信息失败')
      }
      this.editCatesLists = res.data
      console.log(this.editCatesLists)
    }
  },
  created () {
    this.getCateLists()
  }
}
</script>

<style lang="less" scoped>
.el-button {
  margin-bottom: 15px;
}
.el-cascader{
    height: 50px;
    width: 100%;
}

</style>
